import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    private GreenfootImage image1 = null;
    private GreenfootImage image2 = null;
    private int mark = 1;
    
    public Crab()
    {
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage(image1);
    }
    
    public void act() 
    {
        move(3);
        keyControl();
        animation();
        if(isTouching(Worm.class))
        {
            removeTouching(Worm.class);
            Greenfoot.playSound("slurp.wav");
            mark++;
        }
        if(isTouching(Lobster.class))
        {
            Greenfoot.playSound("au.wav");
            Greenfoot.stop();
        }
        if(mark == 4)
        {
            Greenfoot.playSound("fanfare.wav");
            Greenfoot.stop();
        }
    }
    
    public void keyControl() 
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-15);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(15);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            turn(-165);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            turn(165);
        }
    }
    
    public void animation()
    {
        if(getImage() == image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
}


